package itesm.coler.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;

public final class DAOUtil {

    /**
     * Converts the given java.util.Date to java.sql.Date.
     * @param date The java.util.Date to be converted to java.sql.Date.
     * @return The converted java.sql.Date.
     */
	
	private DAOUtil() {
        // Utility class, hide constructor.
    }

	 /**
     * Set the given parameter values in the given PreparedStatement.
     * @param connection The PreparedStatement to set the given parameter values in.
     * @param values The parameter values to be set in the created PreparedStatement.
     * @throws SQLException If something fails during setting the PreparedStatement values.
     */
    public static void setValues(PreparedStatement preparedStatement, Object[] values)
        throws SQLException {
        for (int i = 0; i < values.length; i++) {
            preparedStatement.setObject(i + 1, values[i]);
        }
    }

    /**
     * Converts the given java.util.Date to java.sql.Date.
     * @param date The java.util.Date to be converted to java.sql.Date.
     * @return The converted java.sql.Date.
     */
    public static Date toSqlDate(java.util.Date date) {
     return (date != null) ? new Date(date.getTime()) : null;
    }

    /**
     * Quietly close the Connection. Any errors will be printed to the stderr.
     * @param connection The Connection to be closed quietly.
     */
     public static void close(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                System.err.println("Closing Connection failed: " + e.getMessage());
                e.printStackTrace();
            }
        }
    }
     
     /**
      * Quietly close the Statement. Any errors will be printed to the stderr.
      * @param statement The Statement to be closed quietly.
      */
     public static void close(PreparedStatement statement) {
         if (statement != null) {
             try {
                 statement.close();
             } catch (SQLException e) {
                 System.err.println("Closing Statement failed: " + e.getMessage());
                 e.printStackTrace();
             }
         }
     }

     /**
      * Quietly close the ResultSet. Any errors will be printed to the stderr.
      * @param resultSet The ResultSet to be closed quietly.
      */
     public static void close(ResultSet resultSet) {
         if (resultSet != null) {
             try {
                 resultSet.close();
             } catch (SQLException e) {
                 System.err.println("Closing ResultSet failed: " + e.getMessage());
                 e.printStackTrace();
             }
         }
     }

     /**
      * Quietly close the Connection and Statement. Any errors will be printed to the stderr.
      * @param connection The Connection to be closed quietly.
      * @param statement The Statement to be closed quietly.
      */
     public static void close(Connection connection, PreparedStatement statement) {
         close(statement);
         close(connection);
     }

     /**
      * Quietly close the Connection, Statement and ResultSet. Any errors will be printed to the stderr.
      * @param connection The Connection to be closed quietly.
      * @param statement The Statement to be closed quietly.
      * @param resultSet The ResultSet to be closed quietly.
      */
     public static void close(Connection connection, PreparedStatement statement, ResultSet resultSet) {
         close(resultSet);
         close(statement);
         close(connection);
     }



}
